package com.logistics.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.logistics.common.core.constant.Constants;
import com.logistics.common.core.utils.ServletUtils;
import com.logistics.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.logistics.common.log.annotation.Log;
import com.logistics.common.log.enums.BusinessType;
import com.logistics.common.security.annotation.RequiresPermissions;
import com.logistics.system.domain.Contracts;
import com.logistics.system.service.IContractsService;
import com.logistics.common.core.web.controller.BaseController;
import com.logistics.common.core.web.domain.AjaxResult;
import com.logistics.common.core.utils.poi.ExcelUtil;
import com.logistics.common.core.web.page.TableDataInfo;

/**
 * 合同管理Controller
 * 
 * @author logistics
 * @date 2024-06-23
 */
@RestController
@RequestMapping("/contracts")
public class ContractsController extends BaseController
{
    @Autowired
    private IContractsService contractsService;

    /**
     * 查询合同管理列表
     */
    @RequiresPermissions("system:contracts:list")
    @GetMapping("/list")
    public TableDataInfo list(Contracts contracts)
    {
        startPage();
        List<Contracts> list = contractsService.selectContractsList(contracts);
        return getDataTable(list);
    }

    /**
     * 导出合同管理列表
     */
    @RequiresPermissions("system:contracts:export")
    @Log(title = "合同管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Contracts contracts)
    {
        List<Contracts> list = contractsService.selectContractsList(contracts);
        ExcelUtil<Contracts> util = new ExcelUtil<Contracts>(Contracts.class);
        util.exportExcel(response, list, "合同管理数据");
    }

    /**
     * 获取合同管理详细信息
     */
    @RequiresPermissions("system:contracts:query")
    @GetMapping(value = "/{contractId}")
    public AjaxResult getInfo(@PathVariable("contractId") Long contractId)
    {
        return success(contractsService.selectContractsByContractId(contractId));
    }

    /**
     * 新增合同管理
     */
    @RequiresPermissions("system:contracts:add")
    @Log(title = "合同管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Contracts contracts)
    {
        return toAjax(contractsService.insertContracts(contracts));
    }

    /**
     * 修改合同管理
     */
    @RequiresPermissions("system:contracts:edit")
    @Log(title = "合同管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Contracts contracts)
    {
        return toAjax(contractsService.updateContracts(contracts));
    }

    /**
     * 删除合同管理
     */
    @RequiresPermissions("system:contracts:remove")
    @Log(title = "合同管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{contractIds}")
    public AjaxResult remove(@PathVariable Long[] contractIds)
    {
        return toAjax(contractsService.deleteContractsByContractIds(contractIds));
    }

    /**
     * 托运人签字
     */
    @RequiresPermissions("system:contracts:shipperSign")
    @Log(title = "合同管理", businessType = BusinessType.UPDATE)
    @PostMapping("/shipperSign/{contractId}")
    public AjaxResult shipperSign(@PathVariable Long contractId)
    {
        Contracts contracts = contractsService.selectContractsByContractId(contractId);
        if(!contracts.getContractStatus().equals(Constants.CONTRACTS_STATUS_1)){
            return AjaxResult.error("该合同不是承运人已签字状态，禁止托运人签字");
        }
        contracts.setContractStatus(Constants.CONTRACTS_STATUS_2);
        return toAjax(contractsService.updateContracts(contracts));
    }

    /**
     * 承运人签字
     */
    @RequiresPermissions("system:contracts:carrierSign")
    @Log(title = "合同管理", businessType = BusinessType.UPDATE)
    @PostMapping("/carrierSign/{contractId}")
    public AjaxResult carrierSign(@PathVariable Long contractId) {
        Contracts contracts = contractsService.selectContractsByContractId(contractId);
        if(!contracts.getContractStatus().equals(Constants.CONTRACTS_STATUS_0)){
            return AjaxResult.error("该合同不是未签约状态，禁止承运人签字");
        }
        contracts.setContractStatus(Constants.CONTRACTS_STATUS_1);
        return toAjax(contractsService.updateContracts(contracts));
    }
}
